//
//  WBNetworkManager.swift
//  WeiBo
//
//  Created by gzy on 2018/1/17.
//  Copyright © 2018年 GZY. All rights reserved.
//

import UIKit
//导入框架文件夹的名字
import AFNetworking

// Swift的枚举支持任意数据类型
enum WBHttpMethod {
    case GET
    case POST
}
class WBNetworkManager: AFHTTPSessionManager {
    ///静态区/常量/闭包/
    //在第一次访问时执行闭包并且将结果保存在 shared 常量中
    static let shared = WBNetworkManager()

    
    //使用一个函数封装 AFN 的GET/POST 请求
    func request(method: WBHttpMethod = .GET , URLString: String, parameters: [String: AnyObject], completion: @escaping (_ json: AnyObject?, _ isSuccess: Bool) ->()) {
        
        let success = { (task: URLSessionDataTask, json: AnyObject?)->() in
            completion(json, true)
        }
        let failure = { (task: URLSessionDataTask?, error: NSError)->() in
            completion(nil, false)
            print("网络请求失败 \(error)")
        }
                if method == .GET {
            get(URLString, parameters: parameters, progress:nil , success: success, failure: failure)
            
        }else {
          
            post(URLString, parameters: parameters, progress: nil, success: (success as! (URLSessionDataTask, Any?) -> Void), failure: failure as? (URLSessionDataTask?, Error) -> Void)
        }
    }
    
}
